package safrain.nodelet.template;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import safrain.nodelet.structure.Structure;
import safrain.nodelet.structure.StructureService;

public class TemplateService {
	/**
	 * 模板依赖于结构服务
	 */
	private StructureService structureService;

	public StructureService getStructureService() {
		return structureService;
	}

	public void setStructureService(StructureService structureService) {
		this.structureService = structureService;
	}

	private Map<String, ObjectTemplate> templates = new HashMap<String, ObjectTemplate>();

	/**注册一个模板
	 * @param name 模板名称
	 * @param clazz 模板对应的对象类型
	 * @param values 模板各字段的置，map的key与对象的fieldname相对应 
	 */
	public ObjectTemplate registerTemplate(String name, Class<?> clazz,
			Map<String, Object> values) {
		Structure structure = structureService.getStructure(clazz);

		ObjectTemplate objectTemplate = new ObjectTemplate(structure);
		for (Entry<String, Object> entry : values.entrySet()) {
			objectTemplate.setValue(entry.getKey(), entry.getValue());
		}
		templates.put(name, objectTemplate);
		return objectTemplate;

	}

	public ObjectTemplate getTemplate(String name) {
		return templates.get(name);
	}
}
